const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bigSearchShow:false,
    bigSearchType:"",
    searchDataList:[],
    deptList: [],
    areaList: [],
    dutyDept:"",
    baseUrl: app.globalData.baseUrl,
    tabList:[
      {name:'全部',tag:""},
      {name:'审核',tag:""},
      {name:'整改',tag:""},
      {name:'完成',tag:""},
      {name:'逾期',tag:""}],
    tabIndex:0,
    itemData:{
      
    },
    listData:[],
    searchParams:{}
  },

  setKeyword(e){
    this.setData({
      ['searchParams.keyword']:e.detail.value})
  },

  cancelSelection(){
    this.setData({
      areaIndex: '',
      ['searchParams.area']: ''
    })
  },

  areaChange: function (e) {
    this.setData({
      areaIndex: e.detail.value,
      ['searchParams.area']: this.data.areaList[e.detail.value]
    })
  },

  tabChange(e){
    if(e.detail.tabIndex==0){
      this.setData({
        ['searchParams.type']:'',
        tabIndex:e.detail.tabIndex
      })
    }
    else{
      this.setData({
        ['searchParams.type']:e.detail.tabIndex,
        tabIndex:e.detail.tabIndex
      })
    }
    this.loadData()
  },
  toDetail(e){
    wx.navigateTo({
      url: `/subPages/yinHuanGzDetail/yinHuanGzDetail?riskId=${e.currentTarget.dataset.item.riskId}`,
    })
  },

  loadData(){
    const that = this;
    var userInfo = wx.getStorageSync('userinfo');
    console.log('tt',that.data.searchParams);
    wx.request({
      url: `${that.data.baseUrl}/rmStaff/wx/getRiskList`,
      method: 'GET',
      data:Object.assign({},{
        userSapId: userInfo.siocSapNo,//'区劲林',
        userDept: userInfo.siocDepartCode,
        keyword:'',
        type:'',
        dutyDept:'',
        area: ''
      },that.data.searchParams),
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        if(res.data.code==200){

          let tempTabList = that.data.tabList;
          tempTabList[that.data.tabIndex].tag = `[${res.data.rows.length}]`

          that.setData({
            listData:res.data.rows,
            tabList:tempTabList
          })
          
        }else{
          
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 3000
          });
          return;
          
        }
        console.log('res',res);
      }
    });
  },

  doBigSearchShow(e) {
    console.log('e', e);
    console.log(this.data.deptList.length);
    const tempArray = [];
    const typeMethods = {
      dept: () => {
        if (this.data.deptList.length == 0) {
          wx.showToast({
            title: '暂无部门',
            icon: 'none'
          });
          return;
        } else {
          this.data.deptList.forEach(item => {
            tempArray.push({
              name: item
            });
          });
        }
      }
    }
    typeMethods[e.currentTarget.dataset.type]();

    this.setData({
      bigSearchType: e.currentTarget.dataset.type,
      bigSearchShow: true,
      searchDataList: tempArray
    })
  },
  searchSelect(e) {
    console.log('e1231412313',e);
    this.setData({
      ['searchParams.dutyDept'] : e.detail.name||"",
      dutyDept : e.detail.name||""
    });
    this.loadData();
  },
  getAllDept: function () {
    var that = this;
    wx.request({
      url: that.data.baseUrl + '/deptInfo/wx/getAllDept',
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('获取所有部门信息:', res.data);
        var data = res.data;
        that.setData({
          deptList: data.data
        })
      }
    });
  },
  getAllArea: function () {
    var that = this;
    wx.request({
      url: that.data.baseUrl + '/areaInfo/wx/getAllArea',
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('获取所有区域信息:', res.data);
        var data = res.data;
        that.setData({
          areaList: data.data
        })
      }
    });
  },

  init(){
    this.loadData();
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
    this.getAllDept();
    this.getAllArea();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})